#!/usr/bin/env python
#
#       pytg.py
#
#       Copyright 2008 karthikeyan S.<kaarthikeyapreyan@gmail.com>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import os
import wx
import sys
import shutil
import getopt

import handleargs
import pytestgenerator

try:
    import interface2
except ImportError, E:
    print 'Exception in main: %s'%E

def main():
    """
    Directions
    """
    if  len(sys.argv) <= 1 :
        main_gui()
    else :
        main_cli()

def main_gui():
    """
    GUI Operations
    """
    app = wx.App()
    frame = interface2.BaseFrame()
    frame.SetIcon(wx.Icon\
        ('/usr/share/icons/pytestsgenerator/PyTestGenerator.png',\
        wx.BITMAP_TYPE_PNG))
    frame.Show()
    app.MainLoop()

def main_cli():
    """
    CLI Operations
    """
    try:
        opts, args = getopt.getopt(sys.argv[1:], "ho:m:dw:g",\
            ["help", "output=","module=","write="])
    except getopt.GetoptError, E:
        print str(E)
        sys.exit(2)

    output = None
    mod = None
    write = None
    mypath = None

    generatecase=0
    debug = 0
    enablewrite = 0
    components=[]

    for name in opts :
        components.append(name[0])

    for o, a in opts:
        if o in ("-h", "--help"):
            usage()
            sys.exit()
        if o in ("-o", "--output"):
            output = a.lower()
            if output not in ("all","function","class","method") :
                print "Value set for option -o not recognised"
                sys.exit(2)
        if o in ("-m","--module"):
            mod = a
        if o in ("-w","--write"):
            mypath = a
            enablewrite = 1
        if o in ("-g","--generate"):
            generatecase=1

    if (not mod) or (not output):
        print "Options 'o' and 'm' are mandatory"
        print "use -h or --help to get the USAGE without GUI"
        sys.exit(1)

    verifymod = pytestgenerator.TestGenerator\
        (module=mod,generatefile=generatecase,writefile=enablewrite,writepath=mypath,option=output)
    
    mod_status=verifymod.importcheck(mod)    
    if mod_status[0]:
        verifymod.analyzemod()
    else :
        print "Failure in importing module in importcheck: %s" %mod_status[1]
    
    
def usage() :
    """
    Usage
    """
    print "#########################################################################################"
    print "Invoke GUI"
    print "usage:pytg"
    print "Invoke CLI"
    print "Usage:pytg -m <modname> -o <opts> <options>"
    print "-m <module Name>"
    print "-o <opts>"
    print "-h (or) --help"
    print "-g (or) --generate"
    print "The debug option can be used if you are not sure of what module you are importing"
    print "-w <file Name>"
    print "write option must be used with the filename as option"
    print "Options 'o' and 'm' are Mandatory"
    print "If method is selected it would returned along with the name of the associated class"
    print "______________________________________________________________________________________"
    print "List of opts"
    print "all      --List down the entire description of the module"
    print "class    --List down only the classes in the module"
    print "function --List down only the modules in the module"
    print "method   --List down only the methods in the module"
    print "#########################################################################################"

